package com.example.administrator.testxingpiao.ui.cart;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.testxingpiao.R;
import com.example.administrator.testxingpiao.base.BaseActivity;
import com.example.administrator.testxingpiao.bean.CartBean;
import com.example.administrator.testxingpiao.dao.CartDao;
import com.example.administrator.testxingpiao.dao.OrderDao;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CartActivity extends BaseActivity {

    @BindView(R.id.listview_cart)
    ListView mLv;
    @BindView(R.id.cart_backup)
    ImageView cartBackup;
    @BindView(R.id.cb_allchoose)
    CheckBox cbAllchoose;
    @BindView(R.id.tv_total_amount)
    TextView tvPriceTotal;
    @BindView(R.id.btn_statement)
    Button btnStatement;
    private Double priceTotal = 0.0;
    private List<CartBean> mList;
    private List<CartBean> checkList;
    private boolean selectAll = false;
    private Handler hander = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what == 0){
                mList.clear();
                List<CartBean> cartsBean = CartDao.queryAll(CartActivity.this);
                mList.addAll(cartsBean);
                adapter.notifyDataSetChanged();
            }else if (msg.what == 1) {
                Bundle data = msg.getData();
                priceTotal += data.getDouble("price");
                showPriceTotal();
                //勾选
                CartBean cb = (CartBean) msg.obj;
                if(cb != null){
                    checkList.add(cb);
                }
            }else if (msg.what == 2) {
                Bundle data = msg.getData();
                priceTotal -= data.getDouble("price");
                if(priceTotal < 0){
                    priceTotal = 0.0;
                }
                showPriceTotal();
                //勾选
                CartBean cb = (CartBean) msg.obj;
                if(cb != null){
                    checkList.remove(cb);
                }

            }else if(msg.what == 3) {
                Bundle data = msg.getData();
                priceTotal += data.getDouble("price");
                showPriceTotal();
            }else if(msg.what == 4){
                Bundle data = msg.getData();
                priceTotal -= data.getDouble("price");
                if(priceTotal < 0){
                    priceTotal = 0.0;
                }
                showPriceTotal();
            }
        }
    };
    private CartAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cart);
        ButterKnife.bind(this);
        showPriceTotal();

        mList = new ArrayList<>();
        checkList = new ArrayList<>();
        List<CartBean> cartsBean = CartDao.queryAll(this);
        mList.addAll(cartsBean);
        adapter = new CartAdapter(mList, hander);
        mLv.setAdapter(adapter);

    }

    @OnClick({R.id.cart_backup, R.id.cb_allchoose, R.id.btn_statement})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cart_backup:
                finish();
                break;
            case R.id.cb_allchoose:
                selectAll = !selectAll;
                adapter.SelectAll(selectAll);
                break;
            case R.id.btn_statement:
                for(CartBean cb : checkList){
                    int add = OrderDao.add(cb, this);
                }
                Toast.makeText(CartActivity.this, "结算成功！", Toast.LENGTH_SHORT).show();
                break;
        }
    }
    private void showPriceTotal() {
        tvPriceTotal.setText(String.valueOf(priceTotal));
    }

}
